package gui;

import gui.AddRemoveShipItem.addShip;
import gui.AddRemoveShipItem.reShip;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import users.Manager;

public class AddRemoveHasSong extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = -3532778274566614815L;
	private Manager manager;
	private JTextArea textArea;
	private JScrollPane confirmation;
	private JTextField upcText;
	private JPanel main;
	private JTextField titleText;
	private JButton addSong;
	private JButton reSong;

	public AddRemoveHasSong(Manager manager) {
		this.manager = manager;
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		addPanel();
		confirmPanel();

	}

	private void addPanel() {
		JPanel adder = new JPanel();
		adder.setLayout(new GridLayout(4, 2));
		adder.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

		adder.add(new JLabel("UPC of Item:"));
		upcText = new JTextField();
		adder.add(upcText);

		adder.add(new JLabel("Title of Item:"));
		titleText = new JTextField();
		adder.add(titleText);

		adder.add(new JLabel("Click to insert HasSong tuple"));
		addSong = new JButton("add");
		addSong.addActionListener(new addSong());
		adder.add(addSong);

		adder.add(new JLabel("Click to remove HasSong tuple"));
		reSong = new JButton("remove");
		reSong.addActionListener(new reSong());
		adder.add(reSong);

		add(adder);
	}

	private void confirmPanel() {
		JPanel conPanel = new JPanel(new BorderLayout());
		TitledBorder title;
		title = BorderFactory.createTitledBorder(BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED),
				"Confirmation");
		conPanel.setBorder(title);

		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);

		confirmation = new JScrollPane(textArea);
		confirmation.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		conPanel.add(confirmation, BorderLayout.CENTER);

		add(conPanel);
	}

	public class addSong implements ActionListener {
		String upc, title;

		public void actionPerformed(ActionEvent arg0) {
			upc = upcText.getText();
			title = titleText.getText();
			
			try {
				manager.insertHasSong(upc, title);
				textArea.append("******************* HasSong Inserted ******************" + "\n" +
								"\n" + "UPC: " + upc +
								"\n" + "Title: " + title +
								"\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("HasSong already exists or Associated item does not exist.  Cannot be added!");
				e.printStackTrace();
			}

		}
	}

	public class reSong implements ActionListener {
		String upc, title;

		public void actionPerformed(ActionEvent arg0) {
			upc = upcText.getText();
			title = titleText.getText();
			
			try {
				manager.deleteHasSong(upc, title);
				textArea.append("******************* HasSong Deleted ******************" + "\n"
								+ "\n" + "HasSong tuple with UPC: " + upc + " and Title: " + title + 
								"\n" + "has been removed successfully"
								+ "\n\n"
								+ "*********************************************************"
								+ "\n\n");
			} catch (SQLException e) {
				textArea.append("HasSong does not exist.  Cannot be deleted!");
				e.printStackTrace();
			}

		}

	}
}
